# -*- coding: utf-8 -*- 

'''
Created on Jan 27, 2014

@author: dmen
'''
from mongoengine.fields import *
from mongoengine.connection import connect
from mongoengine.document import Document, EmbeddedDocument
from datetime import datetime
from com.ad.sword import Constants
import mongoengine

connect(Constants.MONGODB_NAME,username=Constants.MONGODB_USERNAME,password=Constants.MONGODB_PASSWORD,host=Constants.MONGODB_URI)



class User(Document):
    username = StringField(max_length=64, unique=True)
    password = StringField(max_length=64)
    email = EmailField()
    description = StringField(max_length=256,  default='')
    last_login_time = DateTimeField(default=datetime(year=1970,month=1,day=2))
    role=ReferenceField('Role')
    class Meta:
        db_table = 'user'
        
class Role(Document):
    name=StringField(max_length=128, unique=True)
    description=StringField(max_length=256)
    class Meta:
        db_table="role"
     
class Feature(Document):
    name=StringField(max_length=128, unique=True)
    description=StringField(max_length=256) 
    code=StringField(max_length=64, unique=True)  
    role=ReferenceField(Role,reverse_delete_rule=mongoengine.NULLIFY)  
    class Meta:
        db_table='feature'    
    
class SystemLog(Document):
    SEVERITY = (
        ('D', 'Debug'),
        ('I', 'Info'),
        ('W', 'Warning'),
        ('E', 'Error'),
        ('C', 'Critical'),
    )
    
    username = StringField(max_length=64)
    ip = StringField(max_length=32)
    severity =StringField(max_length=16,choices=SEVERITY)
    time = DateTimeField(default=datetime.now())
    title = StringField(max_length=128)
    description =StringField(max_length=256)

    class Meta:
        db_table = "system_log"
        
        
class SystemConfig(Document):
    name = StringField(max_length=64, primary_key=True)
    value = StringField()

    class Meta:
        db_table = 'system_config'

class AlarmNotification(Document):
    SEVERITY = (
        ('W', 'Warning'),
        ('E', 'Major'),
        ('C', 'Critical'),
    )
    info=StringField(max_length=256)
    approah=StringField(max_length=16,choices=SEVERITY)
    target_dest=StringField(max_length=256)
    notify_time=DateTimeField(default=datetime.now())
    class Meta:
        db_table = 'alarm_notification'

class MonitorTask(Document):
    STATUS = (
        ('E', 'enable'),
        ('D', 'Disable'),
    )
    TYPES = (
        ('0', 'HTTP'),
        ('1', 'PING'),
        ('2', 'FTP'),
        ('3', 'TCP'),
        ('4', 'UDP'),
        ('5', 'SMTP'),
        ('6', 'MYSQL'),
        ('7', 'APACHE'),
        ('8', 'SERVER'),
    )
    name = StringField(max_length=64)
    desc = StringField(max_length=256)
    frequency=IntField(1)
    type=StringField(max_length=16,choices=TYPES)
    ip=StringField(max_length=64)
    port=IntField()
    status=StringField(max_length=16,choices=STATUS)
    create_time=DateTimeField(default=datetime(year=1970,month=1,day=2))
    class Meta:
        db_table = 'monitor_task'

class Alarm(Document):
    SEVERITY = (
        ('W', 'Warning'),
        ('E', 'Major'),
        ('C', 'Critical'),
    )
    info=StringField(max_length=256)
    task=ReferenceField(MonitorTask,reverse_delete_rule=mongoengine.CASCADE)
    is_history=BooleanField(False)
    severity =StringField(max_length=16,choices=SEVERITY)
    create_time=DateTimeField(default=datetime.now())
    act_time=DateTimeField(default=datetime.now())
    is_actted=BooleanField(False)
    class Meta:
        db_table = 'alarm'
        




class HttpTask(Document):
    NOTIFY_TYPES= (
        ('E', 'Email'),
        ('S', 'SMS'),
    )
    task=ReferenceField(MonitorTask,reverse_delete_rule=mongoengine.CASCADE)
    url=URLField()
    alarm_policy=IntField(default=3)
    notify_types=ListField(StringField(choices=NOTIFY_TYPES))
    result=IntField(default=7)
    class Meta:
        db_table = 'http_task'
    
class TcpTask(Document):
    NOTIFY_TYPES= (
        ('E', 'Email'),
        ('S', 'SMS'),
    )
    task=ReferenceField(MonitorTask,reverse_delete_rule=mongoengine.CASCADE)
    alarm_policy=IntField(default=3)
    notify_types=ListField(StringField(choices=NOTIFY_TYPES))
    result=IntField(default=7)  
    class Meta:
        db_table = 'tcp_task'
    

class MysqlTask(Document):
    NOTIFY_TYPES= (
        ('E', 'Email'),
        ('S', 'SMS'),
    )
    task=ReferenceField(MonitorTask,reverse_delete_rule=mongoengine.CASCADE)
    username=StringField(max_length=64)
    password=StringField(max_length=64)
    alarm_policy=IntField(1)
    notify_types=ListField(StringField(choices=NOTIFY_TYPES))
    result=IntField()  
    class Meta:
        db_table = 'mysql_task'

class ServerAlarmPolicy(EmbeddedDocument):
    cpu_over_time= IntField(default=200)
    cpu_over_percent = IntField(default=99)
    mem_over_time= IntField(default=200)
    mem_over = IntField(default=99)
    disk_over= IntField(default=90)
    fail_times=IntField(default=10)
    
class SnmpTask(Document):
    ENCRYP_TYPES= (
        ('M', 'MD5'),
        ('S', 'SHA'),
    )
    NOTIFY_TYPES= (
        ('E', 'Email'),
        ('S', 'SMS'),
    )
    task=ReferenceField(MonitorTask,reverse_delete_rule=mongoengine.CASCADE)
    snmp_ver=StringField(max_length=8)
    snmp_community=StringField(max_length=128)
    snmp_encryption_type=StringField(max_length=8,choices=ENCRYP_TYPES)
    username=StringField(max_length=64)
    password=StringField(max_length=64)
    alarm_policy=EmbeddedDocumentField(ServerAlarmPolicy)
    notify_types=ListField(StringField(choices=NOTIFY_TYPES))
    class Meta:
        db_table = 'snmp_task'
    

    